home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Internet Info 1994 March
/
Internet Info CD-ROM (Walnut Creek) (March 1994).iso
/
answers
/
comp
/
ForthFaq
/
CASE_ENDCASE
< prev
next >
Wrap
Internet Message Format
|
1993-12-15
|
8KB
Path: senator-bedfellow.mit.edu!bloom-beacon.mit.edu!nic.hookup.net!news.kei.com!sol.ctr.columbia.edu!howland.reston.ans.net!gatech!pitt!willett!ForthFAQ
From: ForthFAQ@willett.pgh.pa.us (FAQ account for comp.lang.forth)
Newsgroups: comp.lang.forth,comp.answers,news.answers
Subject: Forth FAQ: CASE,OF,ENDOF,ENDCASE. (l/m 07.Nov.93)
Message-ID: <4828.UUL1.3#5129@willett.pgh.pa.us>
Date: 15 Dec 93 01:39:59 GMT
Expires: Wed, 22 Dec 93 23:59:59 EDT
References: <4819.UUL1.3#5129@willett.pgh.pa.us>
Followup-To: poster
Lines: 218
Approved: news-answers-request@MIT.Edu
Xref: senator-bedfellow.mit.edu comp.lang.forth:14693 comp.answers:3024 news.answers:15821
Archive-name: ForthFaq/CASE_ENDCASE
Last-modified: 07.Nov.93
Version: 1.2
These are messages that I thought deserved to be preserved. -dwp
Recent changes:
1993-11-07 dwp Started change log. Added code from Randolph Peters,
and comments by john - Rible.
-----------------------
From: eaker@ukulele.crd.ge.com (Chuck Eaker)
Subject: Re: Wanted .. CASE,OF,ENDOF,ENDCASE
Message-ID: <1992Nov25.164255.23225@crd.ge.com>
Date: 25 Nov 92 16:42:55 GMT
In article <jax.722669998@well.sf.ca.us>,
jax@well.sf.ca.us (Jack J. Woehr) writes:
|> In <1992Nov24.101857.84@wronz.org.nz> mentink@wronz.org.nz writes:
|>
|>
|>
|> > Can anyone help with source ( masm/forth) to the CASE statement
|> > word set. I.E .... CASE OF ENDOF ENDCASE ....
|>
|> Baden's CASE is in FORTH Dimensions VIII/5.
|>
|> Eaker, who wrote and enduring CASE construct, checks into
|> this newsgroup once and a while. Charles?
|>
|> =jax=
|> --
|> # jax@well.{UUCP,sf.ca.us} # # Member # # Vice President, #
|> # du!isis!koscej!jax # # X3J14 TC # # Forth Interest Group #
|> # JAX on GEnie # # for ANS # # P.O. Box 8231 #
|> # SYSOP RCFB (303) 278-0364 # # Forth # # San Jose CA 95155 #
1. FIG-Forth
Here is the source for FIG-Forth published with the original article
(Forth Dimensions, Vol. II, No. 3, pp. 37-40.). The ?PAIRS word was
FIG-Forth's way of implementing a small amount of syntax checking.
: CASE ?COMP CSP @ !CSP 4 ; IMMEDIATE
: OF 4 ?PAIRS COMPILE OVER COMPILE = COMPILE OBRANCH
HERE 0 , COMPILE DROP 5 ; IMMEDIATE
: ENDOF 5 ?PAIRS COMPILE BRANCH HERE 0 ,
SWAP 2 [COMPILE] ENDIF 4 ; IMMEDIATE
: ENDCASE 4 ?PAIRS COMPILE DROP
BEGIN SP@ CSP @ = 0=
WHILE 2 [COMPILE ENDIF REPEAT
CSP ! ; IMMEDIATE
1a. Here is additional source for FIG-Forth published in Forth
Dimensions, Vol. III, No. 6, pp. 187-188 in an article by Alfred J.
Monroe. He adds a primitive compiled by OF which reduces the amount
of code compiled by OF. Use the definitions of CASE, ENDOF, and
ENDCASE given above.
: (OF) OVER = IF DROP 1 ELSE 0 ENDIF ;
: OF 4 ?PAIRS COMPILE (OF) COMPILE 0BRANCH
HERE 0 , 5 ; IMMEDIATE
Mr. Monroe also gave code for some interesting variants:
: (<OF) OVER > IF DROP 1 ELSE 0 ENDIF ;
: <OF 4 ?PAIRS COMPILE (<OF) COMPILE 0BRANCH
HERE 0 , 5 ; IMMEDIATE
: (>OF) OVER > IF DROP 1 ELSE 0 ENDIF ;
: >OF 4 ?PAIRS COMPILE (>OF) COMPILE 0BRANCH
HERE 0 , 5 ; IMMEDIATE
: RANGE >R OVER DUP R> 1+ < IF SWAP 1- > IF DROP 1 ELSE 0
ENDIF ELSE DROP DROP 0 ENDIF ;
: RNG-OF 4 ?PAIRS COMPILE RANGE COMPILE 0BRANCH
HERE 0 , 5 ; IMMEDIATE
1b. It is quite common to define (OF) as a CODE word and have
it combine the functions of the run-time (OF) and the compile-time
0BRANCH in the previous definitions. This reduces the amount of
compiled code even more.
CODE (OF) ( 1. Remove the top element of the stack and call it A.
2. If A equals the new top element of the stack,
remove the new top element of the stack,
skip over the branch vector, and execute
the code which follows it.
Else
continue execution at the location indicated
by the branch vector.
) END-CODE
: OF 4 ?PAIRS COMPILE (OF) HERE 0 , 5 ; IMMEDIATE
2. dpANS-3
dpANS-3 contains the following definitions (p. 133) to illustrate
control structure extension. Note that it would be quite easy to
optimize OF along the lines suggested above. Note also that there is no
syntax checking. These words may appear anywhere and not necessarily
combined with each other. In fact, ENDOF may be dispensed with entirely
and replaced with ELSE. Compile-time monitoring of the syntax of
control structure words is a perennial Forth problem.
0 CONSTANT CASE IMMEDIATE ( init count of OFs )
: OF ( #of -- orig #of+1 / x -- )
1+ ( count OFs )
>R ( move off the stack in case the control-flow )
( stack is the data stack. )
POSTPONE OVER POSTPONE = ( copy and test case value )
POSTPONE IF ( add orig to control flow stack )
POSTPONE DROP ( discards case value if = )
R> ; ( we can bring count back now )
IMMEDIATE
: ENDOF ( orig1 #of -- orig2 #of )
>R ( move off the stack in case the control-flow )
( stack is the data stack. )
POSTPONE ELSE
R> ; ( we can bring count back now )
IMMEDIATE
: ENDCASE ( orig 1..orign #of -- )
POSTPONE DROP ( discard case value )
0 ?DO
POSTPONE THEN
LOOP ;
IMMEDIATE
--
Chuck Eaker / P.O. Box 8, K-1 3C12 / Schenectady, NY 12301 USA
eaker@crd.ge.com eaker@crdgw1.UUCP (518) 387-5964
-----------------------
From: sorry no e-mail address (Randolph Peters)
Subject: pocket forth: case...endcase
Message-ID: <sorry?no?e-mail?address-221093104409@meded6.med.upenn.edu>
Date: 22 Oct 93 14:49:04 GMT
What follows is code I have cobbled together to create a
case construct in pocket forth. I have found several instances
where it would be useful to have a multi-branching if, and so
I made this one up. Since I haven't seen the FAQ on case
endcase recently, I am providing this for the benefit of the
pocket forthers out there.
: case 0 ; immediate
: of
[ ' over literal ] compile
[ ' = literal ] compile
[compile] if ; immediate
: endof [compile] else ; immediate
: otherwise ; immediate ( syntactic sugar)
: endcase
begin ?dup while [compile] then repeat
[ ' drop literal ] compile ; immediate
( example of its use )
123 constant SPADE
234 constant HEART
345 constant DIAMOND
456 constant CLUB
: ?cardtype ( n -- )
case
spade of ." It's a spade" cr endof
heart of ." It's a heart" cr endof
diamond of ." It's a diamond" cr endof
club of ." It's a club" cr endof
otherwise ." I don't recognize this suit."
endcase ;
234 ?cardtype
I found this useful in several cases, so please, no flames about
the purity or appropriateness of case...endcase in forth.
Randolph M. Peters
Standard disclaimer.
-----------------------
From: jrible@cup.portal.com (john - Rible)
Subject: Re: pocket forth: case...endcase
Message-ID: <93973@cup.portal.com>
Date: Sat, 23 Oct 93 07:16:05 PDT
Randolph Peters submitted some code for a CASE ... ENDCASE
structure that is significantly different from most of the
implementations that I know of. I apologize for not copying and
changing the code directly, but Portal's online mailer is crude
when used with just Windows terminal program.
Most implementations do a drop AFTER the 'if' in the definition of
OF, removing the case variable from the stack. Then ENDCASE has
its drop BEFORE it resolves the forward references. Both methods
work, but the user better know which style is implemented in
his/her system.
-John
---
If you have any questions about ForthNet/comp.lang.forth or any information
to add/delete or correct in this message or any suggestions on formatting or
presentation, please contact Doug Philips at one of the following addresses:
Internet: dwp@willett.pgh.pa.us
Usenet: ...!uunet!willett.pgh.pa.us!dwp
GEnie: D.PHILIPS3